package code.untils {
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.Capabilities;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	
	/**
	 * @author wenqiang.deng
	 * 
	 * shift键+鼠标左击:生成鼠标捕获对象的投影,可以拖拽移动
	 * ctrl键+鼠标左击:以点击坐标为起点拉出一条直线
	 * c键+鼠标左击:获取点击处的颜色信息
	 */
	public class Ruler
	{
		private static var _stage:Stage;
		private static var _sprite:Sprite;
		private static var _graphics:Graphics;
		private static var _tf:TextField;
		private static var _downPoint:Point;
		private static var _localPoint:Point;
		private static var _isDown:Boolean;
		private static var _keyCode:int = -1;
		
		public static function init(stage:Stage):void
		{
			_stage = stage;
			initSprite();
			initText();
			initEvent();
			_downPoint = new Point();
			_localPoint = new Point();
		}
		
		private static function initSprite():void
		{
			_sprite = new Sprite();
			_graphics = _sprite.graphics;
		}
		
		private static function initText():void
		{
			_tf = new TextField();
			_tf.y = -25;
			_tf.x = -200;
			_tf.width = 400;
			_tf.mouseEnabled = false;
			var tf:TextFormat = _tf.defaultTextFormat;
			tf.align = TextFormatAlign.CENTER;
			tf.color = 0xffffff;
			_tf.defaultTextFormat = tf;
			_tf.filters = [new GlowFilter(0, 1.0, 3.0, 3.0, 5)];
		}
		
		private static function initEvent():void
		{
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			_stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			_stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			_stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private static function onKeyDown(event:KeyboardEvent):void
		{
			_keyCode = event.keyCode;
		}
		
		private static function onKeyUp(event:KeyboardEvent):void
		{
			_keyCode = -1;
			_sprite.parent && _sprite.parent.removeChild(_sprite);
			_tf.parent && _tf.parent.removeChild(_tf);
		}
		
		private static function onMove(event:MouseEvent = null):void
		{
			if (_keyCode == -1 || !_isDown) return;
			_tf.x = _stage.mouseX - 200;
			_tf.y = _stage.mouseY - 25;
			if (_keyCode == Keyboard.SHIFT)
			{
				_sprite.x = _stage.mouseX;
				_sprite.y = _stage.mouseY;
				var dx:int = (_sprite.x - _downPoint.x);
				var dy:int = (_sprite.y - _downPoint.y);
				_tf.text = 'lx:' + (_localPoint.x + dx) //
					+ ' ly:' + (_localPoint.y + dy)//
					+ ' w:' + _sprite.width//
					+ ' h:' + _sprite.height//
					+ ' dx:' + dx//
					+ ' dy:' + dy;
			}
			else if (_keyCode == Keyboard.CONTROL)
			{
				_graphics.clear();
				_graphics.lineStyle(2, 0xff0000);
				_graphics.moveTo(_downPoint.x, _downPoint.y);
				_graphics.lineTo(_stage.mouseX, _stage.mouseY);
				_tf.text = 'gx:' +_downPoint.x +' gx:'+_downPoint.y + ' w:' + Math.abs(_stage.mouseX - _downPoint.x) + ' h:' + Math.abs(_stage.mouseY - _downPoint.y);
			}
		}
		
		private static function onDown(event:MouseEvent):void
		{
			if (_keyCode == -1) return;
			_isDown = true;
			if (_keyCode == Keyboard.SHIFT)
			{
				showRulerSprite();
				var o:InteractiveObject = event.target as InteractiveObject;
				if (o is Stage)
					return;
				var rect:Rectangle = o.getRect(o);
				var p1:Point = o.localToGlobal(new Point(rect.x, rect.y));
				_localPoint.x = o.x;
				_localPoint.y = o.y;
				_graphics.clear();
				_graphics.beginFill(0xff0000, 0.4);
				_graphics.drawRect(p1.x - _stage.mouseX, p1.y - _stage.mouseY, o.width, o.height);
				_graphics.endFill();
			}
			else if (_keyCode == Keyboard.CONTROL)
			{
				showRulerSprite();
				_sprite.x = 0;
				_sprite.y = 0;
				_graphics.clear();
			}
			else if (_keyCode == Keyboard.C)
			{
				showRulerSprite();
				_graphics.clear();
				var r:Rectangle = _stage.getBounds(_stage);
				var bmd:BitmapData = new BitmapData(1,1,true, 0xFFFFFF);
				bmd.draw(_stage,new Matrix(1,0,0,1,-_stage.mouseX,-_stage.mouseY),null,null);
				var color:uint = bmd.getPixel32(0, 0);
				bmd.dispose();
				var format:TextFormat = new TextFormat();
				format.color = color;
				// TODO dwq_封装
				var str:String = '0x';
				for (var i:int = 8;i > 0;i--)
				{
					var count:int = color / Math.pow(16, i - 1);
					if (count < 10)
					{
						str += String(count);
					}
					else if (count == 10)
					{
						str += 'A';
					}
					else if (count == 11)
					{
						str += 'B';
					}
					else if (count == 12)
					{
						str += 'C';
					}
					else if (count == 13)
					{
						str += 'D';
					}
					else if (count == 14)
					{
						str += 'E';
					}
					else if (count == 15)
					{
						str += 'F';
					}
					color -= (Math.pow(16, i - 1) * count);
				}
				_tf.text = 'COLOR:' + str;
				_tf.setTextFormat(format, 0, 6);
			}
			onMove(null);
		}
		
		private static function showRulerSprite():void
		{
			_downPoint.x = _stage.mouseX;
			_downPoint.y = _stage.mouseY;
			_stage.addChild(_sprite);
			_stage.addChild(_tf);
		}
		
		private static function onUp(event:MouseEvent):void
		{
			_isDown = false;
			_sprite.parent && _sprite.parent.removeChild(_sprite);
			_tf.parent && _tf.parent.removeChild(_tf);
		}
	}
}
